package com.hqyj.mall.xiaomimall.controller;

import com.hqyj.mall.xiaomimall.entity.Address;
import com.hqyj.mall.xiaomimall.json.R;
import com.hqyj.mall.xiaomimall.service.AddressService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping("address")
public class AddressController {

    @Resource
    private AddressService addressService;


    @GetMapping("/query/{uid}")
    public R queryUserAddressListByUid(@PathVariable("uid") Integer uid){
        try{
            List<Address> addressList = addressService.queryAddressListByUid(uid);

            return R.success("查询用户收货地址列表成功！",addressList);
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常！");
        }
    }

    @PostMapping("/")
    public R saveUserAddress(@RequestBody Address address){
        try{
            if(addressService.saveAddress(address)){
                return R.success("新增收货地址成功！");
            }
            return R.fail("新增收货地址失败！");
        }catch (Exception ex){
            ex.printStackTrace();
            return R.error("程序出现异常");
        }
    }
}
